package island.gotenyama.estimation;

import island.gotenyama.beans.AppSimpleListItem;
import island.gotenyama.beans.Plan;
import island.gotenyama.beans.UpPlan;
import island.gotenyama.enums.InsType;
import island.gotenyama.enums.PlanRowType;
import island.gotenyama.utils.AppConstants;

import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.ComponentKeyListener;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Keyboard;
import org.apache.pivot.wtk.ListButton;
import org.apache.pivot.wtk.TableView;
import org.apache.pivot.wtk.Theme;
import org.apache.pivot.wtk.Window;

import test.HelloApp;

/**
 * 試算シミュレーションクラス
 * 
 * @author t.kawabata
 */
public class EstimationExplorer implements Application {

	/**
	 * 画面定義ファイルのパス
	 */
	private String BXML_PATH = "/island/gotenyama/estimation/estimation_explorer_window.bxml";

	/**
	 * Windows
	 */
	protected Window window;

	/**
	 * 支店リスト
	 */
	protected ListButton branchList;

	/**
	 * 営業所リスト
	 */
	protected ListButton officeList;

	/**
	 * 品目リスト
	 */
	protected ListButton prodList;

	/**
	 * 上位計画
	 */
	protected TableView upPlanTableView;

	/**
	 * 担当計画
	 */
	protected TableView planTableView;

	@Override
	public void startup(Display display, Map<String, String> properties) throws Exception {

		// 初期化
		Theme.getTheme().setFont(AppConstants.DEFAULT_FONT);

		// 画面ロード
		BXMLSerializer bxml = new BXMLSerializer();
		window = (Window) bxml.readObject(HelloApp.class, BXML_PATH);

		// 支店
		branchList = (ListButton) bxml.getNamespace().get("branchList");
		List<AppSimpleListItem> branches = new ArrayList<AppSimpleListItem>();
		branches.add(new AppSimpleListItem("B01", "東京支店"));
		branches.add(new AppSimpleListItem("B02", "東北支店"));
		branches.add(new AppSimpleListItem("B03", "大阪支店"));
		branches.add(new AppSimpleListItem("B04", "中国支店"));
		branches.add(new AppSimpleListItem("B05", "九州支店"));
		branchList.setListData(branches);
		branchList.setSelectedIndex(0);

		// 営業所
		officeList = (ListButton) bxml.getNamespace().get("officeList");
		List<AppSimpleListItem> offices = new ArrayList<AppSimpleListItem>();
		offices.add(new AppSimpleListItem("O01", "北営業所"));
		offices.add(new AppSimpleListItem("O02", "東営業所"));
		offices.add(new AppSimpleListItem("O03", "西営業所"));
		offices.add(new AppSimpleListItem("O04", "南営業所"));
		officeList.setListData(offices);
		officeList.setSelectedIndex(0);

		// 品目
		prodList = (ListButton) bxml.getNamespace().get("prodList");
		List<AppSimpleListItem> prods = new ArrayList<AppSimpleListItem>();
		prods.add(new AppSimpleListItem("P01", "品目1"));
		prods.add(new AppSimpleListItem("P02", "品目2"));
		prods.add(new AppSimpleListItem("P03", "品目3"));
		prods.add(new AppSimpleListItem("P04", "品目4"));
		prods.add(new AppSimpleListItem("P05", "品目5"));
		prods.add(new AppSimpleListItem("P06", "品目6"));
		prods.add(new AppSimpleListItem("P07", "品目7"));
		prods.add(new AppSimpleListItem("P08", "品目8"));
		prodList.setListData(prods);
		prodList.setSelectedIndex(0);

		// 上位計画
		upPlanTableView = (TableView) bxml.getNamespace().get("upPlanTableView");
		List<UpPlan> upPlanList = new ArrayList<UpPlan>();
		upPlanList.add(new UpPlan(InsType.UH, 1000000, 900000));
		upPlanList.add(new UpPlan(InsType.P, 600000, 600000));
		upPlanList.add(new UpPlan(InsType.UHP, 1600000, 1500000));
		upPlanTableView.setTableData(upPlanList);

		// 担当計画
		planTableView = (TableView) bxml.getNamespace().get("planTableView");
		List<Plan> planList = new ArrayList<Plan>();
		planList.add(new Plan(PlanRowType.OFFICE_SUM, null, "営業所計"));
		planList.add(new Plan(PlanRowType.MEISAI, 1000, "担当者１"));
		planList.add(new Plan(PlanRowType.MEISAI, 1001, "担当者２"));
		planList.add(new Plan(PlanRowType.MEISAI, 1002, "担当者３"));
		planList.add(new Plan(PlanRowType.MEISAI, 1003, "担当者４"));
		planList.add(new Plan(PlanRowType.MEISAI, 1004, "担当者５"));
		planList.add(new Plan(PlanRowType.TEAM_SUM, null, "チーム計"));
		planList.add(new Plan(PlanRowType.MEISAI, 1005, "担当者６"));
		planList.add(new Plan(PlanRowType.MEISAI, 1006, "担当者７"));
		planList.add(new Plan(PlanRowType.MEISAI, 1007, "担当者８"));
		planList.add(new Plan(PlanRowType.MEISAI, 1008, "担当者９"));
		planList.add(new Plan(PlanRowType.MEISAI, 1009, "担当者１０"));
		planList.add(new Plan(PlanRowType.TEAM_SUM, null, "チーム計"));
		planList.add(new Plan(PlanRowType.MEISAI, 1010, "担当者１１"));
		planList.add(new Plan(PlanRowType.MEISAI, 1011, "担当者１２"));
		planList.add(new Plan(PlanRowType.MEISAI, 1012, "担当者１３"));
		planList.add(new Plan(PlanRowType.MEISAI, 1013, "担当者１４"));
		planList.add(new Plan(PlanRowType.MEISAI, 1014, "担当者１５"));
		planList.add(new Plan(PlanRowType.MEISAI, 1015, "担当者１６"));
		planList.add(new Plan(PlanRowType.MEISAI, 1016, "担当者１７"));
		planList.add(new Plan(PlanRowType.MEISAI, 1017, "担当者１８"));
		planList.add(new Plan(PlanRowType.MEISAI, 1018, "担当者１９"));
		planList.add(new Plan(PlanRowType.MEISAI, 1019, "担当者２０"));
		planList.add(new Plan(PlanRowType.MEISAI, 1020, "担当者２１"));
		planList.add(new Plan(PlanRowType.MEISAI, 1021, "担当者２２"));
		planList.add(new Plan(PlanRowType.MEISAI, 1022, "担当者２３"));
		planList.add(new Plan(PlanRowType.MEISAI, 1023, "担当者２４"));
		planList.add(new Plan(PlanRowType.MEISAI, 1024, "担当者２５"));
		planList.add(new Plan(PlanRowType.TEAM_SUM, null, "チーム計"));
		planTableView.getComponentKeyListeners().add(new ComponentKeyListener.Adapter() {
			@Override
			public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
				// Enter
				if (keyCode == 10) {
					TableView.RowEditor rowEditor = planTableView.getRowEditor();
					if (rowEditor != null) {
						if (rowEditor.isEditing()) {
							rowEditor.endEdit(true);
						}
					}
					System.out.println("Enter");
					List<?> dataList = planTableView.getTableData();
					if (dataList != null) {
						int len = dataList.getLength();
						if (len > 0 && (len - 1) != planTableView.getSelectedIndex()) {
							planTableView.setSelectedIndex(planTableView.getSelectedIndex() + 1);
							if (rowEditor != null) {
								if (rowEditor.isEditing()) {
									rowEditor.endEdit(true);
								}
								rowEditor.beginEdit(planTableView, planTableView.getSelectedIndex(), planTableView.getColumnAt(9));
							}
						}
					}
				}

				// F2
				else if (keyCode == 113) {
					System.out.println("F2");
					int selectIndex = planTableView.getSelectedIndex();
					if (selectIndex != -1) {
						TableView.RowEditor rowEditor = planTableView.getRowEditor();
						if (rowEditor != null) {
							if (rowEditor.isEditing()) {
								rowEditor.endEdit(true);
							}
							rowEditor.beginEdit(planTableView, selectIndex, planTableView.getColumnAt(9));
						}
					}
				}
				return false;
			}
		});
		planTableView.setTableData(planList);

		// 画面起動
		window.open(display);
	}

	@Override
	public boolean shutdown(boolean optional) throws Exception {
		if (window != null) {
			window.close();
		}
		return false;
	}

	@Override
	public void suspend() throws Exception {
	}

	@Override
	public void resume() throws Exception {
	}
}
